home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 46 / pascal / meanst.pas next >
Encoding:
Pascal/Delphi Source File  |  1986-08-15  |  1.8 KB  |  57 lines

  1.    {MEAN AND STANDARD DEVIATION        GARY CURTIS NEWPORT
  2.  
  3.     PURPOSE: This procedure reads a set of (real or integer) values from
  4.              an array and calculates the mean and (unbiased) standard
  5.              deviation of that set.
  6.  
  7.     ALGORITHM: See Alan R. Miller, Pascal Programs for Scientists and
  8.                Engineers, SYBEX, 1981,.  pp. 21-29.
  9.  
  10.     USE : The following declarations are used:
  11.  
  12.           CONST  max = (some integer);
  13.           TYPE   XarrayType = array[1..max] of integer; (or real)
  14.           VAR    Xarray : XarrayType;
  15.                  Mean, Std_Dev : real;
  16.                  N  : integer;
  17.  
  18.  
  19.            Include MEANSTD
  20.            after declarations.  Fill Xarray with N values,
  21.            and call
  22.                    MEANSTD(Xarray, N, Mean, Std_Dev);
  23.  
  24.            The appropriate values will be stored in Mean and Std_Dev.
  25.  
  26. }
  27.  
  28.    procedure MEANSTD
  29.                     (VAR  x     : XarrayType;
  30.                           N     : integer;
  31.                      VAR  Mean  : real;
  32.                      VAR  Sigma : real );
  33.  
  34.       VAR
  35.          i      : integer;
  36.          sum_x,
  37.          sum_sq : real;
  38.  
  39.       BEGIN {MEANSTD}
  40.       sum_x := 0;
  41.       sum_sq := 0;
  42.       FOR i := 1 to N do
  43.          BEGIN
  44.          sum_x := sum_x + x[i];
  45.          sum_sq := sum_sq + (x[i] * x[i]);
  46.          END; {FOR}
  47.       Mean := sum_x/N;
  48.       Sigma := SQRT((sum_sq - ((SQR(sum_x))/N))/(N - 1))
  49.  
  50.        {    NOTE : The usual definition of the standard deviation uses
  51.                   N, rather than N - 1, in the denominator.  See
  52.                   William L. Hays, Statistics for the Social sciences,
  53.                   New York: Holt, Rinehart and Winston, 1973.
  54.                   pp. 283-285 for a discussion of the difference.  }
  55.  
  56.       END; {MEANSTD}
  57.